import time

from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from Common.Utils import ChatDriver
from selenium.webdriver.support import expected_conditions as EC


class ForumEnroll:
    url = ""
    driver = ""

    def __init__(self):
        self.url = "http://127.0.0.1:9580/sign-in.html"
        self.driver = ChatDriver.driver
        self.driver.get(self.url)
        self.driver.maximize_window()
        self.driver.find_element(By.CSS_SELECTOR,
                                 "body > div > div > div > div:nth-child(1) > div > div.text-center.text-muted.mt-3 > a").click()

    # 注册元素的检查
    def ForumEnrollByTest(self):
        # 分别查找用户名输入框、昵称输入框、密码输入框、确认密码输入框、同意协议按钮、注册按钮
        self.driver.find_element(By.CSS_SELECTOR, "#username")
        self.driver.find_element(By.CSS_SELECTOR, "#nickname")
        self.driver.find_element(By.CSS_SELECTOR, "#password")
        self.driver.find_element(By.CSS_SELECTOR, "#passwordRepeat")
        self.driver.find_element(By.CSS_SELECTOR, "#policy")
        self.driver.find_element(By.CSS_SELECTOR, "#submit")

    def clear_input_fields(self):
        """清除用户名、昵称、密码、确认密码输入框"""
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#nickname").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#passwordRepeat").clear()

    def fill_form_and_submit(self, username, nickname, password, password_repeat):
        """填写表单并提交"""
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys(username)
        self.driver.find_element(By.CSS_SELECTOR, "#nickname").send_keys(nickname)
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys(password)
        self.driver.find_element(By.CSS_SELECTOR, "#passwordRepeat").send_keys(password_repeat)
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        self.driver.find_element(By.CSS_SELECTOR, "#signUpForm > div > h2")  # 注册失败依旧在用户注册界面
        time.sleep(0.5)
        # 添加屏幕截图
        ChatDriver.getScreenShort()
        # ChatDriver.get_alert_image()

    # 正常注册测试
    def ForumEnrollSucTest(self):
        # 清除用户名、昵称、密码、确认密码输入框
        self.clear_input_fields()
        # 正确用户名、正确昵称、正确密码、正确确认密码
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("warriorsss")
        self.driver.find_element(By.CSS_SELECTOR, "#nickname").send_keys("wwww")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123")
        self.driver.find_element(By.CSS_SELECTOR, "#passwordRepeat").send_keys("123")
        self.driver.find_element(By.CSS_SELECTOR, "#policy").click()
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        # 注册成功自动跳转至登录界面
        WebDriverWait(self.driver, 5).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "body > div > div > div > div:nth-child(1) > div > div.card.card-md > div > h2")))
        time.sleep(1)
        # 添加屏幕截图
        ChatDriver.get_alert_image()

    # 异常注册测试
    def ForumEnrollFailTest(self):
        self.driver.find_element(By.CSS_SELECTOR, "#policy").click()
        # 定义所有异常测试用例（包含空值和错误值）
        test_cases = [
            # 0. 正确用户名、正确昵称、正确密码、错误确认密码
            {"username": "warrior", "nickname": "www", "password": "123", "password_repeat": ""},
            # 1. 正确用户名、正确昵称、错误密码、正确确认密码
            {"username": "warrior", "nickname": "www", "password": "", "password_repeat": "123"},
            # 2. 正确用户名、正确昵称、错误密码、错误确认密码
            {"username": "warrior", "nickname": "www", "password": "", "password_repeat": ""},
            # 3. 正确用户名、错误昵称、正确密码、正确确认密码
            {"username": "warrior", "nickname": "", "password": "123", "password_repeat": "123"},
            # 4. 正确用户名、错误昵称、正确密码、错误确认密码
            {"username": "warrior", "nickname": "", "password": "123", "password_repeat": ""},
            # 5. 正确用户名、错误昵称、错误密码、正确确认密码
            {"username": "warrior", "nickname": "", "password": "", "password_repeat": "123"},
            # 6. 正确用户名、错误昵称、错误密码、错误确认密码
            {"username": "warrior", "nickname": "", "password": "", "password_repeat": ""},
            # 7. 错误用户名、正确昵称、正确密码、正确确认密码
            {"username": "", "nickname": "www", "password": "123", "password_repeat": "123"},
            # 8. 错误用户名、正确昵称、正确密码、错误确认密码
            {"username": "", "nickname": "www", "password": "123", "password_repeat": ""},
            # 9. 错误用户名、正确昵称、错误密码、正确确认密码
            {"username": "", "nickname": "www", "password": "", "password_repeat": "123"},
            # 10. 错误用户名、正确昵称、错误密码、错误确认密码
            {"username": "", "nickname": "www", "password": "", "password_repeat": ""},
            # 11. 错误用户名、错误昵称、正确密码、正确确认密码
            {"username": "", "nickname": "", "password": "123", "password_repeat": "123"},
            # 12. 错误用户名、错误昵称、正确密码、错误确认密码
            {"username": "", "nickname": "", "password": "123", "password_repeat": ""},
            # 13. 错误用户名、错误昵称、错误密码、正确确认密码
            {"username": "", "nickname": "", "password": "", "password_repeat": "123"},
            # 14. 全空测试/全错测试
            {"username": "", "nickname": "", "password": "", "password_repeat": ""},
            # 15. 已存在用户名测试
            {"username": "zhangsans", "nickname": "www", "password": "123", "password_repeat": "123"}
        ]

        # 遍历执行所有测试用例
        for case in test_cases:
            # 清空输入框
            self.clear_input_fields()
            # 填写表单并提交
            self.fill_form_and_submit(
                username=case["username"],
                nickname=case["nickname"],
                password=case["password"],
                password_repeat=case["password_repeat"]
            )
